<template>
	<div class="mt-4 category_sort" v-loading="data.mtloading">
		<div v-if="!data.list.length" class="w-full text-center text-inherit">
			{{$t('public.unData')}}
		</div>
		<div v-else>
			<el-tree style="max-width: 600px" ref="treeRef" draggable :data="data.list" :props="defaultProps"
				@node-click="handleNodeClick" />
			<div class="w-1/2 text-left mt-20 ml-6">
				<div class="inline-block">
					<el-button type="primary" @click="submit">{{ $t('public.save')}}</el-button>
					<el-button @click="cancell">{{ $t('public.cancel')}}</el-button>
				</div>
			</div>
		</div>
	</div>
</template>

<script lang="ts" setup>
import { useI18n } from 'vue-i18n' 
const { t: $t } = useI18n()
import { getgoodsGcSort, updategoodsGcSort } from '@/api/machine';
import { useRouter } from "vue-router";
let data = reactive({
	mtloading: false, // 加载
	list: [], // 列表信息
	original: '',
})
interface Tree {
	label: string
	children?: Tree[]
}
const defaultProps = {
	children: 'child',
	label: 'gc_name',
}
const router = useRouter()
let props = defineProps({
	info: {
		type: Object,
		default: {}
	}
})

// 获取列表信息
let getlist = () => {
	let id = props.info.m_id
	data.mtloading = true
	getgoodsGcSort({ m_id: id }).then(res => {
		data.mtloading = false
		if (res.state == 200 && res.data) {
			data.original = JSON.stringify(res.data)
			data.list = res.data
		}
	}).catch(err => {
		data.mtloading = false
	})
}

// 保存
let submit = () => {
	let array = []
	for (let key in data.list) {
		array.push({
			gc_id: data.list[key].gc_id,
			gc_sort: key
		})
	}
	data.mtloading = true
	updategoodsGcSort({ gcList: array }).then(res => {
		data.mtloading = false
		if (res.state == 200) {
			ElMessage({
				message: $t('message.setcomplete'),
				type: 'success'
			})
			getlist()
			return
		}
		ElMessage({
			message: $t('message.networkFailed'),
			type: 'error'
		})
	})
}

// 取消
let cancell = () => {
	data.list = JSON.parse(data.original)
	router.back()
}

getlist()

</script>
<style lang="scss" scoped>
.childShop {
	width: 93.5%;
}
</style>